<?php

namespace App\Models\CacheModel\zl2;

use App\Models\Api\BaseCache;
use think\Log;
use traits\model\SoftDelete;
use AstroTool\Cache\CacheManage;

class Zl2FortuneTarotCard extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl2_fortune_tarot_card';

    protected $cacheModel = 'fortune.tarot_card';

    use SoftDelete;

    /**
     * 转运卡牌 写入
     * @param int $uid
     * @param array $data
     * @return int
     */
    public function saveData(int $uid, array $data)
    {
        try
        {
            $id = $this->insertGetId($data);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel)
                ->setCacheKeyDetail('today_card_id', "{$uid}:{$data['date']}")
                ->setCacheType('string')->setCache($id);

            $this->listDataAdd($uid, [$id => $id]);
        }
        catch (\Exception $e)
        {
            return 0;
        }

        return (int)$id;
    }

    /**
     * 获取今日 转运卡牌ID
     * @param int $uid
     * @param $date
     * @return int
     * @throws \Exception
     */
    public function getCardIdByToday(int $uid, $date)
    {
        $id = CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->setCacheKeyDetail('today_card_id', "{$uid}:{$date}")
            ->setCacheType('string')->getDataAndSet(function () use ($uid, $date)
            {
                try
                {
                    return self::withTrashed()->where(['uid' => $uid, 'date' => $date])->value('id');
                }
                catch (\Exception $e)
                {
                    Log::write('Zl2FortuneTarotCard model => getCardIdByToday()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');

                    return 0;
                }
            }, [], 'today_card_id');

        return $id ? (int)$id : 0;
    }

    /**
     * 批量获取 转运卡牌 数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {
                $data = self::withTrashed()->where(['id' => ['in', $ids]])->order("id {$sortType}")->column('uid,card_id,direction,create_time,date,delete_time', 'id');
            } else
            {
                $data = $this->where(['uid' => $type[0]])->order("id {$sortType}")->column('uid,card_id,direction,create_time,date,delete_time', 'id');
            }

        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'uid', 'card_id', 'direction', 'date', 'create_time']);
        }

        return $data;
    }

    /**
     * 删除 转运卡牌 记录
     * @param int $uid 用户ID
     * @param int $id 转运卡牌 记录ID
     * @return bool
     */
    public function delData(int $uid, int $id): bool
    {
        try
        {
            self::destroy($id);
            $this->listDataDel($uid, [$id]); // 删除列表项
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $id)->rmCache(); // 删除详情缓存
        }
        catch (\Exception $e)
        {
            Log::write('转运卡牌 删除错误：' . $e->getMessage() . "；错误文件「{$e->getFile()}」错误行数「{$e->getLine()}」");
            return false;
        }
        return true;
    }
}
